"""
负责将braumat sql server中的数据推送到ECSB  2024-11-21
"""
import datetime
import json
import sys
import time

from sqlalchemy import text

from connect_ecsb import push_data_to_ecsb
from connector import get_mssql_connection
from get_logger import logger
from helpers import build_dict_handle_dt

UPDATE_TIME_DICT = {
    "sistar_eng_areas": "2024-10-01 00:00:00.000",
    "sistar_eng_enums": "2024-10-01 00:00:00.000",
    "sistar_eng_eops": "2024-10-01 00:00:00.000",
    "sistar_eng_epars": "2024-10-01 00:00:00.000",
    "sistar_eng_ordercat_params": "2024-10-01 00:00:00.000",
    "sistar_eng_pcus": "2024-10-01 00:00:00.000",
    "sistar_eng_reccats": "2024-10-01 00:00:00.000",
    "sistar_eng_recipes": "2024-10-01 00:00:00.000",
    "sistar_eng_units": "2024-10-01 00:00:00.000",
    "sistar_rt_batches": "2000-01-01 00:00:00.000",
    "sistar_rt_steps": "2000-01-01 00:00:00.000",
    "sistar_rt_step_params": "2000-01-01 00:00:00.000"
}

FIRST_RUN = True

if FIRST_RUN:
    with open('./update_time_record.json', 'w', encoding='utf8') as f:
        f.write(json.dumps(UPDATE_TIME_DICT))

FIRST_RUN = False
mssql_conn = get_mssql_connection()


def update_current_ts():
    """
    获取当前更新的时间戳
    :return:
    """
    with open('./update_time_record.json', 'r', encoding='utf8') as f:
        file_data = f.read()
    _current_ts_dict = json.loads(file_data)
    current_ts_dict = {}
    for k, v in _current_ts_dict.items():
        current_ts_dict.setdefault(k, datetime.datetime.strptime(v, '%Y-%m-%d %H:%M:%S.%f'))
    return current_ts_dict


while True:
    try:
        # 更新时间戳
        current_ts = update_current_ts()
        for table_name, current_timestamp in current_ts.items():
            if table_name == 'sistar_rt_step_params' or table_name == 'sistar_rt_steps':
                column_name = "insert_timestamp"
            else:
                column_name = "update_timestamp"
            # 读取数据
            mssql_records_list = []
            try:
                data_counts = mssql_conn.execute(text(f"select count(*) from {table_name};"))
                data_pages = data_counts.fetchall()[0][0] // 500 + 1
            except Exception as e:
                logger.error(f'从sqlserver 读取数据{table_name}报错：{str(e)}')
                continue
            for page in range(1, data_pages + 1):
                try:
                    mssql_records = mssql_conn.execute(
                        text(
                            f"select * from {table_name} where {column_name} > '{current_timestamp}' order by {column_name} ASC OFFSET 0 ROWS FETCH NEXT {500*page} ROWS ONLY;"))
                    mssql_records_list = build_dict_handle_dt(mssql_records.keys(), mssql_records.fetchall())
                except Exception as e:
                    logger.error(f'从sqlserver 读取数据表:{table_name}-page:{page}报错:{str(e)}')
                # 推送数据
                request_data = {
                    "data": str(mssql_records_list),
                    "organizationCode": "NJA",
                    "data_table": table_name,
                    "data_type": "",
                    "udpdate_time": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                }
                try:
                    response_code, response_text = push_data_to_ecsb(request_data=request_data)
                except Exception as e:
                    logger.error(f'数据推送错误:{str(e)}')
                # 更新最新的时间戳
                try:
                    ts_list = [datetime.datetime.strptime(item[column_name], '%Y-%m-%d %H:%M:%S.%f') for item in mssql_records_list]
                    if len(ts_list) > 0:
                        max_timestamp_dict = max(ts_list)
                        # 读取数据库中的最大的时间戳
                        UPDATE_TIME_DICT[table_name] = max_timestamp_dict.strftime('%Y-%m-%d %H:%M:%S')
                    with open('./update_time_record.json', 'w', encoding='utf8') as f:
                        f.write(json.dumps(UPDATE_TIME_DICT))
                except Exception as e:
                    logger.error(f'table_{table_name},更新时间戳失败:{str(e)}')
    except Exception as e:
        logger.error(str(e))
        try:
            mssql_conn.close()
        except Exception as e:
            logger.error(str(e))
        mssql_conn = get_mssql_connection()
    finally:
        print(f'推送成功-{datetime.datetime.now()}')
        time.sleep(5*60)
